﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightRevivePoint : NetUnionFightBase
    {
        Dictionary<long, InstUFRevivePoint> revivePoints = new Dictionary<long, InstUFRevivePoint>();
        public NetUnionFightRevivePoint(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {
        }

        public override void Enter()
        {
            base.Enter();
            revivePoints.Clear();
        }

        public override void Leave()
        {
            base.Leave();
            revivePoints.Clear();
        }

        public override RoomCharactor CreateCharactor(long uuid)
        {
            var inst = revivePoints[uuid];
            charactorManager.CreateUnionRevivePoint(inst);
            revivePoints[uuid] = inst;
            return GetCharactor(uuid+"");
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);

            if (revivePoints.ContainsKey(uuid))
                revivePoints.Remove(uuid);
        }

        /// <summary>
        /// 刷新复活点
        /// </summary>
        /// <param name="msg"></param>
        public void RevivePointRefresh(SCUFRevivePoint msg)
        {
            if (msg != null)
            {
                foreach (var point in msg.point)
                {
                    RevivePointRefresh(point);
                }
            }
        }

        /// <summary>
        /// 刷新复活点
        /// </summary>
        /// <param name="msg"></param>
        public void RevivePointRefresh(InstUFRevivePoint rp)
        {
            var uuid =  rp.guid;
            revivePoints[uuid] = rp;
            states[uuid] = -1;
          
            var charactor = GetCharactor(uuid,false);
            if (charactor == null)
            {
                charactor = CreateCharactor(uuid);
            }

            var ai = charactor.ai as RCAIRevivePoint;
            ai.InitCamp(rp.camp);
            

#if UNITY_EDITOR
            charactor.Camp = rp.camp;
#endif
        }

        /// <summary>
        /// 出生点被占领
        /// </summary>
        /// <param name="msg"></param>
        public void RevivePointOccupy(SCUFRevivePointOccupy msg)
        {
            var uuid = 0L;
            foreach (var point in revivePoints)
            {
                uuid = point.Value.guid;
                if (point.Value.id == msg.id)
                    break;
            }
            revivePoints[uuid].camp = msg.camp;
            var dictData = Dict.Blo.DictUnionFightRevivePointBlo.GetUnionFightRevivePointByID(msg.id + "");

            var charactor = charactorManager.GetCharactor(uuid+"");
            var ai = charactor.ai as RCAIRevivePoint;
            ai.SetCamp(msg.camp);
#if UNITY_EDITOR
            charactor.Camp = msg.camp;
#endif
        }

        /// <summary>
        /// 复活点的更新
        /// </summary>
        /// <param name="msg"></param>
        public void RevivePointUpdate(SCUFRevivePointUpdate msg)
        {
            var uuid =  msg.point.guid;
            //RevivePointRefresh(msg.point);
            
            revivePoints[uuid] = msg.point;
            
            var charactor = GetCharactor(uuid);
            var ai = charactor.ai as RCAIRevivePoint;
            var ui = charactor.ui as RCUIUnionRevivePoint;
            ai.SetCount(msg.redPlayerCount, msg.bluePlayercount);
            ui.SetCampCount(msg.redPlayerCount, msg.bluePlayercount);
            ui.SetSlider((float)msg.point.leftOccupyProgress, (float)msg.point.rightOccupyProgress);
        }
    }
}